<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

define('GNUSOCIAL', true);
define('INSTALLDIR', '.');
set_include_path('../../');
require_once(INSTALLDIR . '/lib/util/framework.php');

final class ApiAccountRegisterActionTest extends TestCase
{
    private $instance;

    protected function setUp(): void
    {
        $GLOBALS['config'] = [];
        $this->instance = new ApiAccountRegisterAction;
    }

    public function testHandle(): void
    {
        $GLOBALS['config']['site'] = ['closed' => true];
        $this->expectExceptionMessage('Registration not allowed.');

        $mock = $this->createMock('ApiAccountRegisterAction');
        $mock->expects($this->any())
            ->method('clientError')
            ->willReturnCallback(function(
                string $msg, int $code = 400, ?string $format = null) {
                throw new \Exception($msg, $code, $format);
        });
        $this->doMethod($mock, 'handle', []);
    }

    /**
     * Run with private method.
     * @param object $object target object.
     * @param string $methodName private method name。
     * @param array $param argument for private method。
     * @return mixed result。
     * @throws \ReflectionException throw when missing argument。
     */
    private function doMethod(object $object, string $methodName, array $param): object
    {
        $controller = $object;
        $reflection = new \ReflectionClass($controller);
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(true);
        return $method->invokeArgs($controller, $param);
    }
}